﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class TaiKhoanDAO
    {
        public TaiKhoanDTO[] LayDanhSachTaiKhoan(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachTaiKhoan", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            TaiKhoanDTO[] list = new TaiKhoanDTO[dt.Rows.Count];
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list[i] = new TaiKhoanDTO();
                    list[i].MaTK = dt.Rows[i]["MTK"].ToString();
                    list[i].TenDangNhap = dt.Rows[i]["TenTK"].ToString();
                    list[i].MatKhau = dt.Rows[i]["MatKhau"].ToString();
                    list[i].CauHoi = dt.Rows[i]["CaHoi"].ToString();
                    list[i].CauTraLoi = dt.Rows[i]["CauTraLoi"].ToString();
                    list[i].HoTen = dt.Rows[i]["HoTen"].ToString();
                    list[i].DiaChi = dt.Rows[i]["DiaChi"].ToString();
                    list[i].DienThoai = dt.Rows[i]["DienThoai"].ToString();
                    list[i].MaQH = dt.Rows[i]["MQH"].ToString();
                    list[i].DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    
                }
            }
            return list;
        }
        public string ThemTaiKhoan(TaiKhoanDTO taiKhoan, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemTaiKhoan", new SqlParameter("@tenTK", taiKhoan.TenDangNhap),
                                                            new SqlParameter("@matKhau", taiKhoan.MatKhau),
                                                            new SqlParameter("@cauHoi", taiKhoan.CauHoi),
                                                            new SqlParameter("@traLoi", taiKhoan.CauTraLoi),
                                                             new SqlParameter("@hoTenKH", taiKhoan.HoTen),
                                                             new SqlParameter("@diaChi", taiKhoan.DiaChi),
                                                             new SqlParameter("@dienThoai", taiKhoan.DienThoai),
                                                             new SqlParameter("@maQuyenHan", taiKhoan.MaQH), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaTaiKhoan(TaiKhoanDTO taiKhoan, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaTaiKhoan", new SqlParameter("@maTK", taiKhoan.MaTK),
                                                            new SqlParameter("@tenTK", taiKhoan.TenDangNhap),
                                                            new SqlParameter("@matKhau", taiKhoan.MatKhau),
                                                            new SqlParameter("@cauHoi", taiKhoan.CauHoi),
                                                            new SqlParameter("@traLoi", taiKhoan.CauTraLoi),
                                                             new SqlParameter("@hoTenKH", taiKhoan.HoTen),
                                                             new SqlParameter("@diaChi", taiKhoan.DiaChi),
                                                             new SqlParameter("@dienThoai", taiKhoan.DienThoai),
                                                             new SqlParameter("@maQuyenHan", taiKhoan.MaQH),
                                                              new SqlParameter("@daXoa", taiKhoan.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaTaiKhoan(TaiKhoanDTO taiKhoan, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaTaiKhoan", new SqlParameter("@maTK", taiKhoan.MaTK),par);
                                                           

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}